package pl.poznan.put.cs.to.tests;

import java.util.ArrayList;
import java.util.List;

import pl.poznan.put.cs.to.tsp.common.FileOperations;
import pl.poznan.put.cs.to.tsp.common.IResultHolder;
import pl.poznan.put.cs.to.tsp.hea.HybridEvolutionaryAlgorithm;
import pl.poznan.put.cs.to.tsp.listeners.GraphicWindow;
import pl.poznan.put.cs.to.tsp.listeners.TSPListener;
import pl.poznan.put.cs.to.tsp.listeners.TSPWindowListener;

public class HEATest {
	private static GraphicWindow graphicWindow;

	private static TSPListener tspListener;

	public static final int MAX_TIME = 90000;

	public static void printList(List<Integer> list) {
		for (int i : list) {
			System.out.print(i + " ");
		}
		System.out.println();
		System.out.println("n = " + list.size());
	}

	public static void main(String[] args) {
		graphicWindow = new GraphicWindow();
		tspListener = new TSPWindowListener(graphicWindow);
		// tspListener = new EmptyTSPListener();

		List<IResultHolder> results = new ArrayList<IResultHolder>();

		for (int i = 0; i < 1; i++) {
			results.add(new HybridEvolutionaryAlgorithm(args[0], "result.txt",
					HEATest.MAX_TIME, tspListener));
		}

		IResultHolder best = null;
		for (IResultHolder irh : results) {
			if (best == null
					|| ((best.getLengthA() + best.getLengthB()) > (irh
							.getLengthA() + irh.getLengthB()))) {
				best = irh;
			}
		}

		double avg = 0;
		for (IResultHolder irh : results) {
			avg += (irh.getLengthA() + irh.getLengthB());
		}
		avg = avg / results.size();

		double stdev = 0;
		for (IResultHolder irh : results) {
			stdev += ((avg - (irh.getLengthA() + irh.getLengthB())) * (avg - (irh
					.getLengthA() + irh.getLengthB())));
		}
		stdev = stdev / results.size();
		stdev = Math.sqrt(stdev);

		System.out.println("best: " + (best.getLengthA() + best.getLengthB()));
		System.out.println("avg: " + avg);
		System.out.println("stdev: " + stdev);
		tspListener.solutionGenerated(best.getListA(), best.getListB(), best
				.getVertexes());

		System.out.println();
		System.out.println("=======RESULT=======");
		System.out.println("A:");
		HEATest.printList(best.getListA());
		System.out.println("B:");
		HEATest.printList(best.getListB());

		FileOperations.saveSolutionToFile(best, "TEST-HEA.txt");
	}
}
